﻿@using Kite.Gateway.Application
@using Kite.Gateway.Application.Contracts
@using Kite.Gateway.Application.Contracts.Dtos.Node
@using Kite.Gateway.Application.Contracts.Dtos
@using System.Linq
@inject ToastService ToastService
@inject IHttpClientFactory HttpClientFactory
@inject INodeAppService NodeAppService
@inject IConfigureAppService ConfigureAppService;

<div class="row">
    <ValidateForm Model="@Model" OnValidSubmit="@OnSaveAsync" OnInvalidSubmit="@OnInvalidSubmitAsync">
            <div class="col-12">
                   <Switch OnColor="Color.Success" @bind-Value="Model.IsReloadAuthentication" DisplayText="是否重新加载身份认证数据" OnText="开启" OffText="关闭"></Switch>
             </div>
             <div class="col-12"  style="margin-top:5px;" >
                   <Switch OnColor="Color.Success" @bind-Value="Model.IsReloadMiddleware" DisplayText="是否重新加载中间件数据" OnText="开启" OffText="关闭"></Switch>
             </div>
            <div class="col-12"  style="margin-top:5px;" >
                 <Switch OnColor="Color.Success" @bind-Value="Model.IsReloadWhitelist" DisplayText="是否重新加载白名单数据" OnText="开启" OffText="关闭"></Switch>
            </div>
            <div class="col-12"  style="margin-top:5px;" >
                 <Switch OnColor="Color.Success" @bind-Value="Model.IsReloadYarp" DisplayText="是否重新加载Yarp相关数据" OnText="开启" OffText="关闭"></Switch>
            </div>
            <div class="row" style="margin-top:10px;">
                <div class="col-12">
                    <Button ButtonType="ButtonType.Submit"  IsAsync="true" Color="Color.Primary">保 存</Button>
                </div>
            </div>
           </ValidateForm>
</div>


@code {

    [Parameter]
    public EventCallback OnClickCallback { get; set; }
    //表单数据
    private ReloadConfigureDto Model { get; set; }
    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();
        Model = new ReloadConfigureDto()
        {
            IsReloadAuthentication=false,
            IsReloadMiddleware=false,
            IsReloadWhitelist=false,
            IsReloadYarp=false
        };
    }
    private Task OnInvalidSubmitAsync(EditContext context)
    {
        return Task.CompletedTask;
    }
    private async Task OnSaveAsync(EditContext context)
    {
        try
        {
            var nodeResult = await NodeAppService.GetAllAsync();
            if (nodeResult.Code == 0 && nodeResult.Data.Any())
            {
                var configureResult = await ConfigureAppService.GetConfigureAsync(Model);
                foreach (var node in nodeResult.Data)
                {
                    var httpClient = HttpClientFactory.CreateClient();
                    httpClient.DefaultRequestHeaders.Add("AccessToken", node.AccessToken);
                    var response = await httpClient.PostAsJsonAsync($"{node.Server}/api/kite/refresh/configure", configureResult.Data);

                    if (response.IsSuccessStatusCode)
                    {
                        var jsonResult = await response.Content.ReadFromJsonAsync<KiteResult>();
                        await ToastService.Success("系统提示", $"节点[{node.NodeName}]配置数据刷新成功-{jsonResult.Message}");
                    }
                    else
                    {
                        var responseContent =await response.Content.ReadAsStringAsync();
                        await ToastService.Warning("系统警告", $"节点[{node.NodeName}]配置数据刷新失败-{responseContent}");
                    }
                }
            }
        }
        catch (Exception ex)
        {
            await ToastService.Error("系统警告", $"节点配置数据刷新失败:{ex.Message}");
        }
    }
}
